//
//  PSFilterThirdQueenTrick.m
//  PrefCore
//
//  Created by Sergey on 6/20/14.
//  Copyright (c) 2014 Play-on-smart. All rights reserved.
//

#import "PSFilter.h"

PSHandHash PSFilterThirdQueenTrick(PSHandHash candidates, PSHandHash declarerCards) {
    PSCardSuit suits[PS_CARD_SUIT_RISING_LEN] = {0};
    size_t size = 0;
    // Determine if the declarer has third queen
    for (int i = 0 ; i != PS_CARD_SUIT_RISING_LEN ; i++) {
        PSCardSuit s = PSCardSuitRising[i];
        if (PSHandKeepSuit(candidates, s) != 0) {
            PSCardRankSet cards = PSHandGetSuit(declarerCards, s);
            int length = PSCardSetSize(cards);
            if (length == 3
            &&  !PSCardSetContainsCard(cards, kMaskAce | kMaskKing)
            &&  PSCardSetContainsCard(cards, kMaskQueen)) {
                suits[size++] = s;
            }
        }
    }
    // If there is no third queen in any suit, return all candidates:
    if (size == 0) {
        return candidates;
    }
    int res = candidates;
    // All remaining suit sets should be removed from candidates,
    // because they can make the third queen to take a trick:
    for (int i = 0 ; i < size ; i++) {
        PSCardSuit s = suits[i];
        res = PSHandRemoveHand(res, PSHandKeepSuit(res, s));
    }
    return res != 0 ? res : candidates;
}

